<?php 

require_once '../include.php';

/**
 * 管理员登录
 * @param unknown_type $sql
 * @return Ambigous <multitype:, multitype:>
 */
function login($username, $password){
	$PdoMySQL=new PdoMySQL;
	$sql="select * from bac_admin where username='{$username}' and password='{$password}'";
	return $PdoMySQL->getRow($sql);
}

/**
 * 检查管理员是否存在
 * @param 
 * @return Ambigous <multitype:, multitype:>
 */
function checkAdmin(){
	$username = $_POST['username'];
	// var_dump($username);
	$PdoMySQL=new PdoMySQL;
	$sql="select * from bac_admin where username='{$username}'";
	// $flag =false; 
	$count = 0;
	$count = count($PdoMySQL->getAll($sql));
	// var_dump($count);
	echo $count > 0?"该管理已存在":"";
}
/**
 * 检测是否有管理员登陆.
 */
function checkLogined(){
	if($_SESSION['adminId']==""){
		alertMes("请先登陆","login.php");
	}
}
/**
 * 添加管理员
 * @return string
 */
function addAdmin($PdoMySQL){
	$arr=$_POST;
	$arr['password']=md5($_POST['password']);

	if($PdoMySQL->add($arr,"bac_admin")){
		$mes="添加成功!<br/><a href='addAdmin.php'>继续添加</a>|<a href='listAdmin.php'>查看管理员列表</a>";
	}else{
		$mes="添加失败!<br/><a href='addAdmin.php'>重新添加</a>";
	}
	return $mes;
}

/**
 * 得到所有的管理员
 * @return array
 */

function getAdminByPage($page,$pageSize=2){
	$PdoMySQL=new PdoMySQL;
	$tables = "bac_admin";
    global $totalRows;
	$totalRows=count($PdoMySQL->find($tables,null,'*',null,null,null,null));
	global $totalPage;
	$totalPage=ceil($totalRows/$pageSize);
	if($page<1||$page==null||!is_numeric($page)){
		$page=1;
	}

	if($page>=$totalPage)$page=$totalPage;
	$offset=($page-1)*$pageSize;

	$rows=$PdoMySQL->find($tables,null,'*',null,null,null,array($offset,$pageSize));

	return $rows;

}

/**
 * 编辑管理员
 * @param int $id
 * @return string
 */
function editAdmin($id){
	$arr=$_POST;
	$arr['password']=md5($_POST['password']);

	$PdoMySQL=new PdoMySQL;
	$table = "bac_admin";

	if($PdoMySQL->update($arr,$table, "id={$id}",'','')){
		$mes="编辑成功!<br/><a href='listAdmin.php'>查看管理员列表</a>";
	}else{
		$mes="编辑失败!<br/><a href='listAdmin.php'>请重新修改</a>";
	}
	return $mes;
}

/**
 * 删除管理员的操作
 * @param int $id
 * @return string
 */
function delAdmin($id){
	$PdoMySQL=new PdoMySQL;
	$table = "bac_admin";
	
	if($PdoMySQL->delete($table,"id={$id}")){
		$mes="删除成功!<br/><a href='listAdmin.php'>查看管理员列表</a>";
	}else{
		$mes="删除失败!<br/><a href='listAdmin.php'>请重新删除</a>";
	}
	return $mes;
}
/**
 * 得到指定ID的管理员
 * @return array
 */
function findAdminById($id){
	$PdoMySQL=new PdoMySQL;
	$tables = "bac_admin";
	// findById($tabName,$priId,$fields='*')
	$row = $PdoMySQL->findById($tables, $id, 'id, username, password, email');
	return $row;
}

/**
 * 注销管理员
 */
function logout(){
	$_SESSION=array();

	session_destroy();
	header("location:login.php");
}


